using Img2ASCII;
using System;
using System.Drawing;
using System.Windows.Forms;

namespace ImageToASCII
{
    public partial class Form1 : Form
    {
        string m_currFilePathSelected;
        public Form1()
        {
            InitializeComponent();

            //点击图片也视同选择图片
            pb_srcPic.Click += pb_srcPic_Click;

            cb_asciiCharSet.SelectedIndex = 0;
        }

        private void pb_srcPic_Click(object sender, EventArgs e)
        {
            var dialog = new OpenFileDialog();
            dialog.Filter = "图片文件(*.jpg,*.jpeg,*.png,*.gif,*.bmp)|*.jpg;*.jpeg;*.png;*.gif;*.bmp";
            dialog.ShowDialog();

            //未选中，则返回
            if (string.IsNullOrEmpty(dialog.FileName))
            {
                return;
            }

            m_currFilePathSelected = dialog.FileName;

            try
            {
                //读入图片：
                pb_srcPic.BackgroundImage = Image.FromFile(dialog.FileName);
            }
            catch (Exception ex)
            {
                MessageBox.Show("读取图片失败：" + ex.Message);
            }

        }

        private void btn_img2ASCII_Click(object sender, EventArgs e)
        {
            string asciiChars = cb_asciiCharSet.Text;

            int width = 100;
            if (!int.TryParse(tb_outputWidth.Text.Trim(), out width))
            {
                MessageBox.Show("长度信息填写请使用数字，不能使用：" + tb_outputWidth.Text);
            }
            else if (string.IsNullOrEmpty(m_currFilePathSelected))
            {
                return;
            }
            else if (string.IsNullOrEmpty(asciiChars))
            {
                return;
            }

            string output = Img2ASCIIHelper.ProcessImage(m_currFilePathSelected, width, asciiChars);
            rtb_outputText.Text = output;
        }
    }
}
